package facatory;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;

public class BeanFactory {
    private static final Map<String, Object> beans = new HashMap<>();
//或许文件对象流
    static {
    InputStream in = BeanFactory.class.getClassLoader().getResourceAsStream("beans.properties");
//   创建配置对象流
    Properties prop = new Properties();
    try {
//        读取到配置文件对象
        prop.load(in);
//        遍历
        Enumeration<?> enumeration = prop.propertyNames();
        while (enumeration.hasMoreElements()) {
            String key = (String) enumeration.nextElement();
            String values = (String) prop.get(key);
//根据值获取类对象
            Class<?> aClass = Class.forName(values);
//            创建类对象
            Object o = aClass.newInstance();
            beans.put(key, o);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
//    获取对象
    public static Object getBeanStr(String key){
        return beans.get(key);
    }
    public static void main(String[] args) {
        Object userDao = BeanFactory.getBeanStr("userDao");
        System.out.println(userDao);
    }
}
